﻿<UserControl x:Class="Samba.Presentation.Terminal.TicketEditorView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:FxButton="clr-namespace:FlexButton;assembly=FlexButton" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:Common="clr-namespace:Samba.Presentation.Common;assembly=Samba.Presentation.Common" mc:Ignorable="d" 
             d:DesignHeight="320" d:DesignWidth="240" Loaded="UserControl_Loaded">

    <UserControl.Resources>
        <Style x:Key="GroupLabelStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>

        <Border BorderThickness="1" Grid.Row="1" Grid.Column="0" BorderBrush="{StaticResource {x:Static SystemColors.ActiveBorderBrushKey}}">
            <ScrollViewer Name="Scroller" Common:KineticBehaviour.HandleKineticScrolling="True" VerticalScrollBarVisibility="Auto">
                <ItemsControl 
                            Name="TicketItemsControl"
                            BorderThickness="0"
                            Focusable="False"
                            Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}"
                            ItemsSource="{Binding SelectedTicket.ItemsViewSource.View}"
                            HorizontalContentAlignment="Stretch">
                    <ItemsControl.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Margin" Value="0,0,0,5"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="auto"/>
                                                        <RowDefinition Height="*"/>
                                                    </Grid.RowDefinitions>
                                                    <Border BorderThickness="0,0,0,1" BorderBrush="Silver">
                                                        <Grid Grid.Row="0">
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="*"/>
                                                                <ColumnDefinition Width="auto"/>
                                                            </Grid.ColumnDefinitions>
                                                            <TextBlock Margin="5,0,0,0" Grid.Column="0" Text="{Binding Name.OrderNumber}" FontWeight="Bold"
                                                                       Foreground="Gray" Style="{StaticResource GroupLabelStyle}" FontSize="10"/>
                                                            <TextBlock Margin="5,0,0,0" Grid.Column="1" Text="{Binding Name.Time}" FontWeight="Bold"
                                                                       Foreground="Gray" Style="{StaticResource GroupLabelStyle}" FontSize="10"/>
                                                        </Grid>
                                                    </Border>
                                                    <ItemsPresenter Grid.Row="1"/>
                                                </Grid>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </ItemsControl.GroupStyle>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Style="{StaticResource ItemsControlButtonStyle}"
                                            Command="{Binding ItemSelectedCommand}"
                                            CommandParameter="{Binding}">
                                <Grid>
                                    <StackPanel>
                                        <Grid Background="{Binding Background}" Margin="-1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="auto"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="auto"/>
                                                <ColumnDefinition Width="auto"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition/>
                                                <RowDefinition/>
                                            </Grid.RowDefinitions>

                                            <TextBlock FontWeight="{Binding FontWeight}" TextDecorations="{Binding TextDecoration}" Padding="1" Foreground="{Binding Foreground}" Grid.Column="0" Text="{Binding Quantity,StringFormat='#.##'}" Margin="5,1,6,1"/>
                                            <TextBlock FontWeight="{Binding FontWeight}" TextDecorations="{Binding TextDecoration}" Padding="1" Foreground="{Binding Foreground}" Grid.Column="1" Text="{Binding Description}" TextTrimming="CharacterEllipsis" Margin="1"/>
                                            <TextBlock FontSize="8" VerticalAlignment="Center" TextDecorations="{Binding TextDecoration}" Foreground="Gray" Grid.Column="2" Text="{Binding PriceTag}" Margin="1,1,0,1" HorizontalAlignment="Right"/>
                                            <TextBlock FontWeight="{Binding FontWeight}" TextDecorations="{Binding TextDecoration}" Padding="1" Foreground="{Binding Foreground}" Grid.Column="3" Text="{Binding TotalPrice, StringFormat='#,#0.00'}" Margin="1,1,3,1" HorizontalAlignment="Right"/>
                                            <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Text="{Binding Reason}" Margin="0,-7,3,0"  Foreground="{Binding Foreground}" HorizontalAlignment="Right">
                                                 <TextBlock.Style>
                                                    <Style TargetType="TextBlock">
                                                        <Style.Triggers>
                                                            <DataTrigger Value="" Binding="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Text}">
                                                                <Setter Property="Visibility" Value="Collapsed"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                        </Grid>

                                        <ItemsControl ItemsSource="{Binding Properties}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*"/>
                                                            <ColumnDefinition Width="auto"/>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Grid.Column="0" Foreground="DarkBlue" Text="{Binding DisplayString}" Margin="30,0,0,0" />
                                                        <TextBlock Grid.Column="1" Foreground="DarkBlue" Text="{Binding PriceString}" Padding="1" Margin="0,0,3,0" HorizontalAlignment="Right"/>
                                                    </Grid>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>
                                    <Rectangle Margin="1,1,2,1" x:Name="Rectangle" Fill="Transparent" Stroke="{StaticResource {x:Static SystemColors.HotTrackBrushKey}}"
                                                                   StrokeDashCap="Square" StrokeThickness="{Binding BorderThickness}" SnapsToDevicePixels="True">
                                        <Rectangle.StrokeDashArray>
                                            <sys:Double>5</sys:Double>
                                        </Rectangle.StrokeDashArray>
                                    </Rectangle>
                                </Grid>
                            </Button>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Border>

        <Border Grid.Row="2" Grid.Column="0" Background="{StaticResource {x:Static SystemColors.InfoBrushKey}}" 
                        BorderBrush="{StaticResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" 
                        BorderThickness="1,0,1,1" Visibility="{Binding IsTicketNoteVisible,Converter={StaticResource VisibilityConverter},FallbackValue=Collapsed}">
            <TextBlock Margin="3,2,3,3" TextTrimming="WordEllipsis" Text="{Binding Note}"/>
        </Border>

        <UniformGrid Columns="2" Grid.Row="3" Grid.Column="0" Margin="0,2,2,0">
            <UniformGrid.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontSize" Value="10"/>
                </Style>
            </UniformGrid.Resources>
            <TextBlock Text="{LocText TicketTotal,Suffix=':'}" Visibility="{Binding IsTicketDiscountVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock TextAlignment="Right" Text="{Binding SelectedTicket.TicketPlainTotalLabel}" Visibility="{Binding IsTicketDiscountVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock Text="{LocText DiscountTotal,Suffix=':'}" Foreground="DarkRed" FontWeight="Bold" Visibility="{Binding IsTicketDiscountVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock TextAlignment="Right" Foreground="DarkRed" FontWeight="Bold" Text="{Binding SelectedTicket.TicketDiscountLabel}" Visibility="{Binding IsTicketDiscountVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock Text="{LocText Total,Suffix=':'}" Visibility="{Binding IsTicketTotalVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock TextAlignment="Right" Text="{Binding SelectedTicket.TicketTotalLabel}" Visibility="{Binding IsTicketTotalVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock Text="{LocText PaymentTotal,Suffix=':'}" Visibility="{Binding IsTicketPaymentVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock TextAlignment="Right" Text="{Binding SelectedTicket.TicketPaymentLabel}" Visibility="{Binding IsTicketPaymentVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock FontSize="12" FontWeight="Bold" Foreground="Red" Text="{LocText Balance,Suffix=':'}" Visibility="{Binding IsTicketRemainingVisible,Converter={StaticResource VisibilityConverter}}"/>
            <TextBlock FontSize="12" FontWeight="Bold" Foreground="Red" TextAlignment="Right" Text="{Binding SelectedTicket.TicketRemainingLabel}" Visibility="{Binding IsTicketRemainingVisible,Converter={StaticResource VisibilityConverter}}"/>
        </UniformGrid>

        <StackPanel Orientation="Horizontal" Grid.Row="4" Grid.ColumnSpan="2">
            <ItemsControl ItemsSource="{Binding PrintJobButtons,NotifyOnTargetUpdated=True}" HorizontalAlignment="Stretch">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <FxButton:FlexButton 
                                    Focusable="False"    
                                    Command="{Binding DataContext.PrintJobCommand,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" 
                                    CommandParameter="{Binding Model}"
                                    MinHeight="26" MinWidth="45"
                                    Margin="1,1,0,0">
                            <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Caption}"/>
                        </FxButton:FlexButton>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

            <ItemsControl ItemsSource="{Binding TicketTagButtons,NotifyOnTargetUpdated=True}" HorizontalAlignment="Stretch">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Stretch" Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <FxButton:FlexButton 
                                    Focusable="False"    
                                    Command="{Binding DataContext.TicketTagCommand,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" 
                                    CommandParameter="{Binding Model}"
                                    MinHeight="26" MinWidth="45"
                                    ButtonColor="{Binding ButtonColor}"
                                    Margin="1,1,0,0">
                            <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Caption}"/>
                        </FxButton:FlexButton>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>

        <StackPanel Grid.Column="1" Grid.RowSpan="3" Grid.Row="0" VerticalAlignment="Top">
            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding AddMenuItemsCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding AddMenuItemsCommand.Caption}"/>
            </FxButton:FlexButton>
            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding DeleteSelectedItemsCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding DeleteSelectedItemsCommand.Caption}"/>
            </FxButton:FlexButton>

            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding IncSelectedQuantityCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding IncSelectedQuantityCommand.Caption}"/>
            </FxButton:FlexButton>
            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding DecSelectedQuantityCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding DecSelectedQuantityCommand.Caption}"/>
            </FxButton:FlexButton>

            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding ChangeTableCommand}" Height="26" Width="43" Visibility="{Binding IsTableButtonVisible,Converter={StaticResource VisibilityConverter}}">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding ChangeTableCommand.Caption}"/>
            </FxButton:FlexButton>
            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding MoveSelectedItemsCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding MoveSelectedItemsCommand.Caption}"/>
            </FxButton:FlexButton>
            <FxButton:FlexButton Margin="1,0,0,1" Command="{Binding EditTicketNoteCommand}" Height="26" Width="43">
                <TextBlock FontWeight="Bold" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding EditTicketNoteCommand.Caption}"/>
            </FxButton:FlexButton>


        </StackPanel>
    </Grid>
</UserControl>
